package tcp.updateandcopy;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.List;
import java.util.Scanner;

public class Client {

	public static void main(String[] args) throws UnknownHostException, IOException {
		// TODO Auto-generated method stub
		Socket socket=new Socket("127.0.0.1",8887);
		OutputStream os=socket.getOutputStream();
		InputStream is=socket.getInputStream();
		BufferedReader bufR=new BufferedReader(new InputStreamReader(is));
//		System.out.println(bufR.readLine());
//	    final  String localPath="\\client";
//	    Scanner sc=new Scanner(System.in);
//	    String command=""; //record the client command
	    //Internet Parma
		//获取help document 
//		System.out.println(bufR.readLine());
//		String input="";  //read file server data
//		while(sc.hasNext())
//		{
//			 command=sc.nextLine();
//			 //commandList存储 lPath,fPath,ip,commandKind
//			 List<String> commandList=Client.commandPraser(command);
//			 //建立套接字，在本次命令末尾关闭套接字（重要）
//			 Socket socket = null;
//			 OutputStream os = null;
//			 InputStream is = null;
//			 BufferedReader bufR = null;
//			 String lPath=""; //local path
//			 String fPath=""; //further path
//			 String commandKind="";
//			 
//			 //建立连接后写格式化后的命令
//			 //server分两次读取判断
//			 os.write((commandKind+"\n").getBytes()); //CP or SCP
//			 os.write((fPath+"\n").getBytes()); //
//			 if(commandKind.equals("CP"))
//			 {
//				 while((input=bufR.readLine())!=null)
//				 {
//					 if(input.startsWith("file found"))
//					 {
//						 //get local Path
//						 String lp=localPath+lPath;
//						 System.out.println("client get file: "+fPath);
//						 OutputStream fileOutput = new FileOutputStream(lp);
//						 System.out.println("file creat success");
//						 while((input=bufR.readLine())!=null)
//						 {
//							 System.out.println(input);
//							 if(input.equals("file end"))
//								 break;
//							 fileOutput.write(input.getBytes());
//							 fileOutput.write("\n".getBytes());
//						 }
//						 fileOutput.close();
//					 }
//					 else
//					 {
//						 System.out.println("the file you want not found ! Correct the name or path and input what you want");
//					 }
//					 break;
//				 }
//			 }
//			 else if(commandKind.equals("SCP"))
//			 {
//				//get local Path
//				String lp=localPath+lPath;
//				System.out.println("client delivery file: "+lPath);
//				//catch server local file and delivery
//				InputStream fileInput = null;
//				try {
//					fileInput = new FileInputStream(lPath);
//				} catch (FileNotFoundException e) {
//					// TODO Auto-generated catch block
//					os.write("not found file\n".getBytes());
//					continue;
//				}
//				os.write("file found\n".getBytes());
//				byte[] buf=new byte[fileInput.available()];
//				fileInput.read(buf);
//				os.write(buf);
//				os.write("file end\n".getBytes());
//				fileInput.close();  //release temporary InputStream
//			 }
//			 else
//			 {
//				 //指令出错在commandPraser中处理
//			 }
//		
//			 //every time close the resouce
//			 is.close();
//			 os.close();
//			 bufR.close();
//			 socket.close();
//		}
//		command=sc.nextLine();
		os.write("SCP\n".getBytes());
		os.write("test.data\n".getBytes());
		InputStream fileInput = null;
//		FileOutputStream fileOutput = new FileOutputStream(fPath);
		try {
			fileInput = new FileInputStream("data.txt");
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			os.write("not found file\n".getBytes());
		}
		byte[] buf=new byte[fileInput.available()];
		fileInput.read(buf);
		os.write(buf);
		System.out.println(new String(buf));
		os.write("\n\rfile end".getBytes());
		fileInput.close();  //release temporary InputStream
		
		socket.close();
	}
	
	public static List<String> commandPraser(String command)
	{
		return null; //not command
		
	}

}
